package com.xyazm.product.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyazm.common.utils.PageUtils;
import com.xyazm.common.utils.Query;
import com.xyazm.product.dao.BrandDao;
import com.xyazm.product.dao.CategoryBrandRelationDao;
import com.xyazm.product.dao.CategoryDao;
import com.xyazm.product.entity.BrandEntity;
import com.xyazm.product.entity.CategoryBrandRelationEntity;
import com.xyazm.product.entity.CategoryEntity;
import com.xyazm.product.service.BrandService;
import com.xyazm.product.service.CategoryBrandRelationService;

@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity>
		implements CategoryBrandRelationService {

	@Autowired
	private BrandDao brandDao;
	@Autowired
	private CategoryDao categoryDao;
	@Autowired
	private CategoryBrandRelationDao relationDao;
	@Autowired
	private BrandService brandService;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		IPage<CategoryBrandRelationEntity> page = this.page(new Query<CategoryBrandRelationEntity>().getPage(params),
				new QueryWrapper<CategoryBrandRelationEntity>());

		return new PageUtils(page);
	}

	@Transactional
	@Override
	public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
		Long brandId = categoryBrandRelation.getBrandId();
		Long catelogId = categoryBrandRelation.getCatelogId();
		BrandEntity brandEntity = brandDao.selectById(brandId);
		CategoryEntity categoryEntity = categoryDao.selectById(catelogId);

		categoryBrandRelation.setBrandName(brandEntity.getName());
		categoryBrandRelation.setCatelogName(categoryEntity.getName());
		this.save(categoryBrandRelation);

	}

	@Override
	public void updateBrand(Long brandId, String brandName) {
		CategoryBrandRelationEntity categoryBrandRelation = new CategoryBrandRelationEntity();
		categoryBrandRelation.setBrandId(brandId);
		categoryBrandRelation.setBrandName(brandName);

		this.update(categoryBrandRelation, new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
	}

	@Override
	public void updateCategory(Long catId, String catName) {
		CategoryBrandRelationEntity categoryBrandRelation = new CategoryBrandRelationEntity();
		categoryBrandRelation.setCatelogId(catId);
		categoryBrandRelation.setCatelogName(catName);

		this.update(categoryBrandRelation, new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
	}

	@Override
	public List<BrandEntity> getBrandsByCatId(Long catId) {

		List<CategoryBrandRelationEntity> catelogId = relationDao
				.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
//        List<BrandEntity> collect = catelogId.stream().map(item -> {
//            Long brandId = item.getBrandId();
//            BrandEntity byId = brandService.getById(brandId);
//            return byId;
//        }).collect(Collectors.toList());

		List<Long> collect = catelogId.stream().map(item -> {
			return item.getBrandId();
		}).collect(Collectors.toList());
		if (collect != null && collect.size() > 0) {
			List<BrandEntity> listByIds = (List<BrandEntity>) brandService.listByIds(collect);
			return listByIds;
		}
		return new ArrayList<BrandEntity>();
	}
}